package com.atlyn.web;

import com.atlyn.bean.Classroom;
import com.atlyn.bean.Plan;
import com.atlyn.bean.Teacher;
import com.atlyn.bean.TeacherClassroomTime;
import com.atlyn.dao.FuncDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class Func1Servlet extends BaseServlet{
    FuncDao funcDao = new FuncDao();

    public void SelectAllClassroom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Classroom> classrooms = funcDao.func1();
        request.setAttribute("classrooms",classrooms);
        request.getRequestDispatcher("/func1.jsp").forward(request,response);
    }
    public void SelectClassroomById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String clnum = request.getParameter("searchmsg");
        Classroom classroom = funcDao.search(clnum);
        if(classroom != null){
            request.setAttribute("classroom",classroom);
        }else {
            request.setAttribute("msg","没有此教室信息！");
        }

        request.getRequestDispatcher("/func1.jsp").forward(request,response);
    }
    public void AddClassroom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clnum = request.getParameter("clnum");
        String type = request.getParameter("type");
        String medium = request.getParameter("medium");
        int capacity = Integer.parseInt(request.getParameter("capacity"));
        System.out.println(clnum+type);

        boolean flag = funcDao.addClassroom(clnum,type,medium,capacity);
        if (flag){
            System.out.println("添加成功！");
            response.sendRedirect("http://localhost:8080/ClassroomManageSystem_war_exploded/func1Servlet?action=SelectAllClassroom");
        }else {
            System.out.println("添加失败！");
            request.setAttribute("msg","添加失败！");
            request.getRequestDispatcher("/add_classroom.jsp").forward(request,response);

        }

    }
    public void DeleteClassroom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进来了");
        String clnum = request.getParameter("clnum");
        System.out.println(clnum);
        funcDao.deleteClassroom(clnum);
        response.sendRedirect("http://localhost:8080/ClassroomManageSystem_war_exploded/func1Servlet?action=SelectAllClassroom");
    }
    public void getClassroom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clnum = request.getParameter("clnum");
        System.out.println(clnum);
        Classroom classroom = funcDao.search(clnum);
        System.out.println(classroom);
        request.setAttribute("classroom",classroom);
        request.getRequestDispatcher("/edit_classroom.jsp").forward(request,response);
    }
    public void UpdateClassroom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String clnum = request.getParameter("clnum");
        String type = request.getParameter("type");
        String medium = request.getParameter("medium");
        int capacity = Integer.parseInt(request.getParameter("capacity"));
        funcDao.editClassroom(clnum,type,medium,capacity);
        response.sendRedirect("http://localhost:8080/ClassroomManageSystem_war_exploded/func1Servlet?action=SelectAllClassroom");
    }
    public void SelectAllPlan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<TeacherClassroomTime> teacherClassroomTimes = funcDao.SelectAllPlan();
        request.setAttribute("list",teacherClassroomTimes);
        request.getRequestDispatcher("/showAllPlan.jsp").forward(request,response);
    }
    public void addPlan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hhh我来安排课程啦");
        String planum = request.getParameter("planum");
        String clnum = request.getParameter("clnum");
        int tid = Integer.parseInt(request.getParameter("tid"));
        int tim = funcDao.PlanCount() + 1;
        String timeid = "time" + tim;
        String startString = request.getParameter("start");
        Date start = funcDao.StringtoDate(startString);
        String endString = request.getParameter("end");
        Date end = funcDao.StringtoDate(endString);
        if (funcDao.addPlan(planum,clnum,tid,timeid,start,end) == true){
            request.setAttribute("msg","成功安排课时！");
        }else {
            request.setAttribute("msg","添加失败！");
        }
        request.getRequestDispatcher("/addPlan.jsp").forward(request,response);
    }
    public void selectAllTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Teacher> teachers = funcDao.selectAllTeacher();
        request.setAttribute("teachers",teachers);
        request.getRequestDispatcher("/showAllTeacher.jsp").forward(request,response);
    }
    public void SelectTeacherByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String tname = request.getParameter("searchmsg");
        List<Teacher> teachers = funcDao.selectTeahcerByName(tname);
        if(teachers != null){
            request.setAttribute("teacherSearch",teachers);
        }else {
            request.setAttribute("msg","没有此教师信息！");
        }

        request.getRequestDispatcher("/showAllTeacher.jsp").forward(request,response);
    }
}
